package org.example.domain.user_raffle.service.engine.handler;

import org.example.domain.user_raffle.model.entity.ActivityStrategyPrizeEntity;
import org.example.domain.user_raffle.repository.IUserRaffleRepo;
import org.example.domain.user_raffle.service.engine.RaffleHandlerNode;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.stream;

/**
 * @author lxc18
 * @date 2025/3/29 20:19
 * @description GuaranteeHandler
 */
@Component
public class GuaranteeHandler extends RaffleHandlerNode {
    @Resource
    private IUserRaffleRepo userRaffleRepo;
    @Override
    public List<Long> doHandler(Long userId, Long activityId, int usedQuota,Map<String, ActivityStrategyPrizeEntity> strategyPrizeEntities) {
        ActivityStrategyPrizeEntity guarantee = strategyPrizeEntities.get("guarantee");
        if (guarantee != null){
            //保底积分
            int guaranteeQuota = Integer.parseInt(guarantee.getConfig());
//            int userUsedQuota = userRaffleRepo.queryUserUsedQuota(activityId, userId);
            if (usedQuota < guaranteeQuota){
                return next.doHandler(userId, activityId,usedQuota,strategyPrizeEntities);
            }
            //保证奖品
            String[] guaranteePrizes = guarantee.getPrizeIds().split(",");
            List<Long> prizeIds = new ArrayList<>();
            stream(guaranteePrizes).forEach(prizeId -> prizeIds.add(Long.parseLong(prizeId)));
            return prizeIds;
        }
        return next.doHandler(userId, activityId,usedQuota,strategyPrizeEntities);
    }
}
